package test.yzx.pproject.act1;

import android.animation.Animator;
import android.animation.AnimatorSet;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.DecelerateInterpolator;

import test.yzx.pproject.R;
import test.yzx.pproject.ViewAnimatorUtil;

public class Anim2Activity extends AppCompatActivity {

    private View circleView;
    private View iconView;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.anim2);
        circleView = findViewById(R.id.view_circle);
        iconView = findViewById(R.id.view2);
    }

    @Override
    public void onEnterAnimationComplete() {
        super.onEnterAnimationComplete();
        getWindow().getDecorView().postDelayed(new Runnable() {
            public void run() {

                AnimatorSet set = new AnimatorSet();
                set.playSequentially(makeCircleAnim(circleView));
                set.playSequentially(makeIconAnim(iconView));
                set.start();

                AnimatorSet upset = new AnimatorSet();
                upset.playTogether(makeTranslateAnim());
                upset.playTogether(makeTranslateBottomAnim());
                upset.setStartDelay(2270);
                upset.start();

            }
        }, 0);
    }


    /* 头部上移 */
    private Animator[] makeTranslateAnim(){
        int duration = 800;
        return new Animator[]{
                ViewAnimatorUtil.make(duration, 0, null, new ViewAnimatorUtil.SimpleValueCallback(){
                    public void onUpdate(Animator animation, float value) {
                        circleView.setTranslationY(value);
                        iconView.setTranslationY(value);
                    }
                }, 0, -180*3),
                ViewAnimatorUtil.make(duration, 0, null, new ViewAnimatorUtil.SimpleValueCallback(){
                    public void onUpdate(Animator animation, float value) {
                        circleView.setScaleY(value); circleView.setScaleX(value);
                        iconView.setScaleY(value); iconView.setScaleX(value);
                    }
                }, 1f, 0.6f)
        };
    }

    /* 底部上滑 */
    private Animator makeTranslateBottomAnim(){
        final View container = findViewById(R.id.container);
        int height = (getResources().getDisplayMetrics().heightPixels - 25*3 - circleView.getMeasuredHeight())/2 - 180*3 +(int)(circleView.getMeasuredHeight()-circleView.getMeasuredHeight()*0.4/2);
        int topMargin = height + 40*3;
        ((ViewGroup.MarginLayoutParams)container.getLayoutParams()).topMargin = topMargin;
        container.requestLayout();
        final View layout = findViewById(R.id.bottomLayout);
        layout.setTranslationY(container.getMeasuredHeight() - topMargin);
        return ViewAnimatorUtil.make(800, 0, new DecelerateInterpolator(0.8f), new ViewAnimatorUtil.SimpleValueCallback(){
            public void onStart(Animator animation) {
                layout.setVisibility(View.VISIBLE);
            }
            public void onUpdate(Animator animation, float value) {
                layout.setTranslationY(value);
            }
        }, container.getMeasuredHeight() - topMargin, 0);
    }

    /* 线圈的scale */
    private Animator[] makeCircleAnim(final View view){
        ViewAnimatorUtil.SimpleValueCallback callback = new ViewAnimatorUtil.SimpleValueCallback(){
            public void onUpdate(Animator animation, float value) {
                view.setScaleX(value); view.setScaleY(value);
            }
        };
        Animator anim1 = ViewAnimatorUtil.make(400, 0, new AccelerateInterpolator(), new ViewAnimatorUtil.SimpleValueCallback(){
            public void onUpdate(Animator animation, float value) {
                view.setScaleX(value); view.setScaleY(value);
            }
            public void onStart(Animator animation) {
                circleView.setVisibility(View.VISIBLE);
            }
        }, 0f, 1.3f);
        anim1.setStartDelay(200);
        return new Animator[]{
                anim1,
                ViewAnimatorUtil.make(600, 0, new DecelerateInterpolator(), callback, 1.3f, 0.7f),
                ViewAnimatorUtil.make(600, 0, new AccelerateInterpolator(), callback, 0.7f, 1.1f),
                ViewAnimatorUtil.make(270, 0, new DecelerateInterpolator(), callback, 1.1f, 1f),
        };
    }

    /* 图标的scale */
    private  Animator[] makeIconAnim(final View view){
        ViewAnimatorUtil.SimpleValueCallback callback = new ViewAnimatorUtil.SimpleValueCallback(){
            public void onUpdate(Animator animation, float value) {
                view.setScaleX(value); view.setScaleY(value);
            }
        };
        return new Animator[]{
                ViewAnimatorUtil.make(500, 0, new AccelerateInterpolator(), new ViewAnimatorUtil.SimpleValueCallback(){
                    public void onUpdate(Animator animation, float value) {
                        view.setScaleX(value); view.setScaleY(value);
                    }
                    public void onStart(Animator animation) {
                        circleView.setVisibility(View.GONE);
                    }
                }, 0f, 1.3f),
                ViewAnimatorUtil.make(900, 0, new DecelerateInterpolator(), callback, 1.3f, 0.5f),
                ViewAnimatorUtil.make(500, 0, new AccelerateInterpolator(), callback, 0.5f, 1.2f),
                ViewAnimatorUtil.make(270, 0, new DecelerateInterpolator(), callback, 1.2f, 1f),
        };
    }


}
